﻿using System;
using System.Globalization;
using System.Windows.Data;

namespace Black.Minecraft.Editor.Converters
{
    /// <summary>
    /// Convertie une date en chaîne de caractère
    /// </summary>
    public class DecimalToStringConverter : IValueConverter
    {
        /// <summary>
        /// Convertie la valeur
        /// </summary>
        /// <param name="value">Valeur à convertir</param>
        /// <param name="targetType">Type cible</param>
        /// <param name="parameter">Paramètre pour la convertion</param>
        /// <param name="language">Culture de l'interface</param>
        /// <returns>Valeur convertie</returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo language)
        {
            if (value != null)
            {
                if (parameter != null && parameter is string && !string.IsNullOrWhiteSpace((string)parameter))
                    return (((decimal)value).ToString((string)parameter));

                return (((decimal)value).ToString());
            }

            return (null);
        }

        /// <summary>
        /// Convertie la valeur en retour
        /// </summary>
        /// <param name="value">Valeur à convertir</param>
        /// <param name="targetType">Type cible</param>
        /// <param name="parameter">Paramètre pour la convertion</param>
        /// <param name="language">Culture de l'interface</param>
        /// <returns>Valeur convertie</returns>
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo language)
        {
            decimal output;

            if (value != null)
            {
                if (decimal.TryParse(value.ToString().Replace(",", "."), NumberStyles.Any, CultureInfo.InvariantCulture, out output))
                    return (output);
            }

            return (null);
        }
    }
}
